# -*- coding: utf-8 -*-
"""
Created on Sat Dec  4 21:44:07 2021

@author: Administrator
"""

# 导入模块
import pygame
from sys import exit
import os

# 初始化部分
pygame.init()

# 设置游戏窗口
screen = pygame.display.set_mode((1024,600))
pygame.display.set_caption("Tank Game")
background = pygame.image.load("./pic/background.jpg").convert()


# 坦克精灵类
tank1_image = pygame.image.load('./pic/tank1.png').convert_alpha()
tank2_image = pygame.image.load('./pic/tank2.png').convert_alpha()
tank3_image = pygame.image.load('./pic/tank3.png').convert_alpha()


class HeroTank(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.tank1_image = tank1_image
        self.tank1_rect = self.tank1_image.get_rect()
        self.tank2_image = tank2_image
        self.tank2_rect = self.tank2_image.get_rect()
        self.tank3_image = tank3_image
        self.tank3_rect = self.tank3_image.get_rect()
        self.speed = 5

    def moveLeft(self):
        if self.tank1_rect.left > 0:      #限制rect的范围，不至于坦克移出屏幕
            self.tank1_rect.x -= self.speed
        self.rotate(0)

    def moveRight(self):
        if self.tank1_rect.right < 1024:
            self.tank1_rect.x += self.speed
        self.rotate(180)

    def moveUp(self):
        if self.tank1_rect.top > 0:
            self.tank1_rect.y -= self.speed
        self.rotate(270)

    def moveDown(self):
        if self.tank1_rect.bottom < 600:
            self.tank1_rect.y += self.speed
        self.rotate(90)


    def rotate(self, angle):
        # 旋转坦克1机身
        self.tank1_image = pygame.transform.rotate(tank1_image, angle)
        self.tank1_rect = self.tank1_image.get_rect(center=self.tank1_rect.center)
        # 旋转坦克1机身
        self.tank2_image = pygame.transform.rotate(tank2_image, angle)
        self.tank2_rect = self.tank2_image.get_rect(center=self.tank2_rect.center)
        # 旋转机身3
        self.tank3_image = pygame.transform.rotate(tank3_image, angle)
        self.tank3_rect = self.tank3_image.get_rect(center=self.tank3_rect.center)


    def display(self,screen,model=1):
        self.tank2_rect.center = self.tank1_rect.center #坦克2中心点和坦克机生rect的中心点保持一致
        self.tank3_rect.center = self.tank1_rect.center #坦克3中心点和坦克机生rect的中心点保持一致
        if model == 1:
            screen.blit(self.tank1_image, self.tank1_rect)
        elif model == 2:
            screen.blit(self.tank2_image, self.tank2_rect)
        elif model == 3:
            screen.blit(self.tank3_image, self.tank3_rect)

# 子弹图片
#bomb_image = pygame.image.load('bomb.png').convert_alpha()
#bomb_rect = bomb_image.get_rect()

# 子弹精灵类
class Explode(pygame.sprite.Sprite):
    def __init__(self):
        self.frame_width = 0
        self.frame_height = 0
        self.rect = 300,300,0,0
        self.columns = 0
        self.last_frame = 1
        self.last_time = 0
        self.frame = 0
        self.first_frame = 0
        self.old_frame = 0

    def load(self, filename, row, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()   # 载入整张图片
        self.master_rect = self.master_image.get_rect()                   # 获取图片的rect值
        self.frame_width = self.master_rect.width//columns                # 计算单一帧的宽度
        self.frame_height = self.master_rect.height//row                  # 计算单一帧的高度
        self.rect = 300, 300, self.frame_width, self.frame_height             # 更新rect
        self.columns = columns                                            # 存储列的值（用以后续计算）
        self.last_frame = row * columns - 1                               # 计算是有0开始的，需要 -1

    def update(self, screen):
        self.frame += 1                                                   # 帧序号 +1
        if self.frame > self.last_frame:
            self.frame = self.first_frame                                 # 循环播放
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width           # 计算 subsurface 的 x 坐标
            frame_y = (self.frame // self.columns) * self.frame_height         # 计算 subsurface 的 y 坐标
            rect = pygame.Rect(frame_x, frame_y, self.frame_width, self.frame_height) # 获取subsurface 的 rect
            self.image = self.master_image.subsurface(rect)                    # 更新self.image
            self.old_frame = self.frame                                        # 更新self.old_frame
        screen.blit(self.image, self.rect)                                     # 显示图像


my_tank = HeroTank()
my_explode = Explode()
my_explode.load('./pic/bomb.png', 4, 8)


framerate = pygame.time.Clock() #实 例化一个时钟对象

mRunning = True
model = 1
while mRunning:
    framerate.tick(30)   #设置每秒帧率为30
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mRunning = False
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_u:
                model = 2
            if event.key == pygame.K_i:
                model = 3
            if event.key == pygame.K_o:
                model = 1


    key_press = pygame.key.get_pressed()
    K_w = pygame.K_w
    K_a = pygame.K_a
    K_s = pygame.K_s
    K_d = pygame.K_d
    K_u = pygame.K_u
    K_i = pygame.K_i

    if key_press[K_w]:
        my_tank.moveUp()
    elif key_press[K_s]:
        my_tank.moveDown()
    elif key_press[K_a]:
        my_tank.moveLeft()
    elif key_press[K_d]:
        my_tank.moveRight()

    screen.blit(background, (0, 0))
    my_tank.display(screen,model)
    my_explode.update(screen)
    pygame.display.update()


